package cn.edu.suda.catalina.dataupdate.util;

import java.lang.reflect.Method;

/**
 * Created by admin on 2017/5/18.
 */
public class ReflectUtil {
    private String filePath;

    public ReflectUtil(String filePath) {
        this.filePath = filePath;
    }

    public void run(String className, String... args) {
        loadClass(className, args);
    }

    /**
     * 反射实例化类
     */
    private void loadClass(String className, String... args) {
        ClassLoaderUtil classLoaderUtil = new ClassLoaderUtil(filePath);
        try {
            Class<?> cls = classLoaderUtil.loadClass(className);
            Object obj = cls.newInstance();
            Method setUp = cls.getDeclaredMethod("setUp");
            Method test = cls.getDeclaredMethod("test", String[].class);
            Method tearDown = cls.getDeclaredMethod("tearDown");
            setUp.invoke(obj);
            test.invoke(obj, (Object) args);
            tearDown.invoke(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
